package com.rahman.doctor;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import android.app.Activity;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ScrollView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;
import java.util.HashMap;

import android.widget.SimpleAdapter;

public class Medicine extends Activity 
{		
	private ListView list1;
	private SimpleAdapter adapter;
	private Button b1, b2, b3;
	private EditText e1;
	private Dialog d;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.medlist);
        b1 = (Button)findViewById(R.id.btnmedicine);
        b2 = (Button)findViewById(R.id.sendMsg);
        
        d = new Dialog(Medicine.this);
        d.setContentView(R.layout.msgdialog);
        e1 = (EditText)d.findViewById(R.id.txtNo);
        b3 = (Button)d.findViewById(R.id.btnSend);
        list1 = (ListView) findViewById( R.id.list );        
        adapter = new SimpleAdapter( this, list, R.layout.medicalist, new String[] {"name","type"}, new int[] {R.id.lblname,R.id.lbldetail});
        populateList();
        list1.setAdapter(adapter);
        startBtnClickListener();

    }
    
    public void startBtnClickListener()
    {
    	b1.setOnClickListener(new View.OnClickListener()
    	{
    		public void onClick(View v)
    		{
    			Intent i = new Intent(Medicine.this, AddMedicine.class);
		        startActivity(i);
    		}
    	});
    	
    	b2.setOnClickListener(new View.OnClickListener()
    	{
    		public void onClick(View arg0)
    		{
    	        MsgDialog();
    		}		
    	});
    }
    
    public void MsgDialog()
    {
  
        d.setTitle("Enter phone number:");              
        b3.setOnClickListener(new View.OnClickListener()
        {
        	@Override
        	public void onClick (View v)
        	{
        		SendMsg();
        		d.dismiss();
        	}
        });
        d.show();
    }
    
    public void SendMsg()
    {
    	if (!(e1.getText().toString().equals("")))
    	{
	    	String phoneNo = e1.getText().toString();
	    	String sms = Message();
	    	
	    	try 
	    	{
				SmsManager smsManager = SmsManager.getDefault();
				smsManager.sendTextMessage(phoneNo, null, sms, null, null);
				Toast.makeText(getApplicationContext(), "SMS Sent!", Toast.LENGTH_LONG).show();
			} 
	    	catch (Exception e) 
	    	{
				Toast.makeText(getApplicationContext(),"SMS failed, please try again later!", Toast.LENGTH_LONG).show();
				e.printStackTrace();
			}
    	}
    }
    
    
    static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>(); 
    
    private void populateList() 
    {
    	list.clear();
    	for (MedicineClass md: MedicineMethods.getMedicineList())
    	{
        	HashMap<String,String> temp = new HashMap<String,String>();
        	temp.put("name", md.getMedName());
        	temp.put("type", md.getMedType());
        	list.add(temp);
    	}
    }
    
    public String Message()
    {
    	String msg = "";
    	
    	for (PrescriptionClass p: PrescriptionMethods.getPrescriptionList())
    	{
    		msg = "#TFM$" + p.getClinicName().toString() + "$" + p.getPatientName().toString() + "$" + p.getIllness().toString() + "$" + p.getDoctorName().toString() + ";";
    	}
    	
    	for (MedicineClass m: MedicineMethods.getMedicineList())
    	{
    		msg += m.getMedName() + "&" + m.getMedType() + "&" + m.getDays() + "&" + m.getToTake() + "&" + Integer.toString(m.getTakingTime()) + "&" + m.getAbFood() + ";;";
    	}
		Toast.makeText(getApplicationContext(),msg.toString(), Toast.LENGTH_LONG).show();

    	return msg.toString();
    }
}
